<template>
    <view class="bg-content">
        <div v-if="tbStoreClassGroupList.length">
            <u-sticky>
                <div style="height: 49px; border-bottom: 1rpx solid #eeeeee; background-color: #FFFFFF;">
                    <u-tabs :list="tbStoreClassGroupList" :gutter="20" active-color="red" :current="activeTabIndex"
                            @change="onTabChange"></u-tabs>
                </div>
            </u-sticky>
        </div>

        <adModule
                v-if="globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig && globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig.isShowAd"></adModule>

        <systemClassLevel2MenusModule
                v-if="globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig && globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig.isShowSystemClassLevel2"></systemClassLevel2MenusModule>

        <div v-if="globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig && globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig.isShowSeckillGoods"
             style="background: white; font-size: 16px; padding: 10px; font-weight: bold;">
            <div style="display: flex">
                <div>
                    <image src="/pages/sub/sub1-tabs/static/icon/tabbar/seckill_light.png" style="width: 20px; height: 20px;"></image>
                </div>
                <div style="padding-left: 5px;">
                    {{globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig.seckillGoodsTitle}}
                </div>
            </div>

        </div>

        <div style="border: 1px solid #eeeeee;"
             v-if="globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig && globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig.isShowSeckillGoods">
            <seckillGoodsModule :seckillGoodsTitle="globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig.seckillGoodsTitle"></seckillGoodsModule>
        </div>

        <div v-if="globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig && globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig.isShowGoodsRecommend"
             style="background: white; font-size: 16px; padding: 10px; font-weight: bold;">
            <div style="display: flex">
                <div>
                    <image src="/pages/sub/sub1-tabs/static/icon/goods_favor.png" style="width: 20px; height: 20px;"></image>
                </div>
                <div style="padding-left: 5px;">
                    {{globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig.goodsRecommendTitle}}
                </div>
            </div>
        </div>
        <div style="border: 1px solid #eeeeee;"
             v-if="globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig && globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig.isShowGoodsRecommend">
            <goodsRecommendModule></goodsRecommendModule>
        </div>

        <div v-if="globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig && globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig.isShowStoreRecommend"
             style="background: white; font-size: 16px; padding: 10px; font-weight: bold;">
            <div style="display: flex">
                <div>
                    <image src="/pages/sub/sub1-tabs/static/icon/shop.png" style="width: 20px; height: 20px;"></image>
                </div>
                <div style="padding-left: 5px;">
                    {{globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig.storeRecommendTitle}}
                </div>
            </div>
        </div>
        <div style="border: 1px solid #eeeeee;"
             v-if="globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig && globalVariable.userInfo.tbCommunity.tbCommunityHomeConfig.isShowStoreRecommend">
            <storeRecommendModule></storeRecommendModule>
        </div>

        <goodsList ref="goodsListRef" :goodsListFilterParams="goodsListFilterParams"
                   v-if="tbStoreClassGroupList.length && tbStoreClassGroupList[activeTabIndex].type !== '1'"></goodsList>

        <tbStoreList ref="tbStoreListRef" :tbStoreListFilterParams="tbStoreListFilterParams"
                     v-if="tbStoreClassGroupList.length && tbStoreClassGroupList[activeTabIndex].type === '1'"></tbStoreList>
    </view>
</template>

<script>
    import adModule from '@/components/home-config/ad-module.vue'
    import systemClassLevel2MenusModule from '@/components/home-config/system-class-level2-menus-module.vue'
    import seckillGoodsModule from '@/components/home-config/seckill-goods-module/seckill-goods-module.vue'
    import goodsRecommendModule from '@/components/home-config/goods-recommend-module/goods-recommend-module.vue'
    import storeRecommendModule from '@/components/home-config/store-recommend-module/store-recommend-module.vue'
    import goodsList from '@/components/goods-list.vue'
    import tbStoreList from '@/components/store-list.vue'
    import {TbStoreClassService} from "@/dev/services/tb-store-class.service";

    export default {
        data() {
            return {
                activeTabIndex: 0,
                tbStoreClassList: [],
                tbStoreClassGroupList: [],
                tbStoreClassItemList: [],
                goodsListFilterParams: {
                    _timestamp: new Date().getTime(),
                    communityId: this.globalVariable.userInfo.tbCommunity.id,
                    searchKeyword: null,
                    parentStoreClassId: null,
                    storeClassId: null,
                    orderBy: 'salesvolume desc'
                },
                tbStoreListFilterParams: {
                    _timestamp: new Date().getTime(),
                    communityId: this.globalVariable.userInfo.tbCommunity.id,
                    searchKeyword: null,
                    parentStoreClassId: null,
                    storeClassId: null,
                    orderBy: 'coordinate desc'
                }
            }
        },
        components: {
            adModule,
            systemClassLevel2MenusModule,
            seckillGoodsModule,
            goodsRecommendModule,
            storeRecommendModule,
            goodsList,
            tbStoreList,
        },
        mounted() {
            const _self = this;
            // 如果还没选客户，则先选择客户。
            if (_self.globalVariable.userInfo.tbCommunityUser.type === "0" && !_self.globalVariable.userInfo.tbCommunityUserType) {  // type=0业务员，1-客户
                console.log(_self.globalVariable.userInfo)
                uni.redirectTo({
                    url: '/pages/common/login-community-user-select/index'
                });
            } else {
                new TbStoreClassService().findTbStoreClassList({
                    communityId: this.globalVariable.userInfo.tbCommunity.id,
                    grade: 1,
                    status: '1',
                    deleted: 0,
                    orderBy: 'display_order asc',
                    returnKeys: 'id,name,grade,type'
                }).then(result => {
                    console.log(result);
                    _self.tbStoreClassList = result;
                    _self.tbStoreClassGroupList = [{id: null, name: '全部', grade: 1}, ..._self.tbStoreClassList.filter(item => item.grade === 1)];
                    _self.$forceUpdate();
                })
            }
        },
        methods: {
            onTabChange(index) {
                this.activeTabIndex = index
                this.goodsListFilterParams.parentStoreClassId = this.tbStoreClassGroupList[this.activeTabIndex].id;
                this.tbStoreListFilterParams.parentStoreClassId = this.tbStoreClassGroupList[this.activeTabIndex].id;
            },
            loadMore() {
                if (this.tbStoreClassGroupList[this.activeTabIndex].type !== '1') {
                    if (this.$refs.goodsListRef.pagination.hasNextPage) {
                        this.$refs.goodsListRef.doInfinite();
                    }
                } else if (this.tbStoreClassGroupList[this.activeTabIndex].type === '1') {
                    if (this.$refs.tbStoreListRef.pagination.hasNextPage) {
                        this.$refs.tbStoreListRef.doInfinite();
                    }
                }
            }
        }
    }
</script>

<style lang="scss" scoped>

</style>
